<?php

namespace Kunfeng\Dify\Model\Common;

/**
 * 元数据模型
 */
class Metadata
{
    private $data;

    public function __construct(array $data = [])
    {
        $this->data = $data;
    }

    /**
     * 从数组创建元数据
     */
    public static function fromArray(array $data): self
    {
        return new self($data);
    }

    /**
     * 获取使用量信息
     */
    public function getUsage(): ?Usage
    {
        return isset($this->data['usage']) 
            ? Usage::fromArray($this->data['usage']) 
            : null;
    }

    /**
     * 获取检索资源
     */
    public function getRetrieverResources(): array
    {
        return $this->data['retriever_resources'] ?? [];
    }

    /**
     * 支持动态属性访问
     */
    public function __get(string $name)
    {
        return $this->data[$name] ?? null;
    }

    public function __set(string $name, $value): void
    {
        $this->data[$name] = $value;
    }

    public function __isset(string $name): bool
    {
        return isset($this->data[$name]);
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return $this->data;
    }
}